package com.jt.controller;

import com.jt.pojo.Item;
import com.jt.service.itemService;
import com.jt.vo.ImageVo;
import com.jt.vo.ItemVO;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@RestController
@CrossOrigin
@RequestMapping("/item/")
public class ItemController {
    @Autowired
    private itemService itemService;


    @DeleteMapping("file/deleteFile")
    public SysResult deleteFile(String virtualPath){
        File file = new File(virtualPath);
        file.delete();
        return SysResult.success();
    }

    /**
     *  MultipartFile针对文件上传开发的API
     * @param file
     * @return
     */
    @PostMapping("file/upload")
    public SysResult upload(MultipartFile file){
        ImageVo upload = itemService.upload(file);

        if (upload==null)return SysResult.fail();
        return SysResult.success(upload);
    }

    @PostMapping("saveItem")
    public SysResult saveItem(@RequestBody ItemVO itemVO){
        itemService.saveItem(itemVO);
        return SysResult.success();
    }

    @PutMapping("updateItem")
    public SysResult updateItem(@RequestBody Item item){
        itemService.updateItem(item);
        return SysResult.success();
    }

    @GetMapping("getItemList")
    public SysResult getItemList(PageResult pageResult) {
        pageResult = itemService.getItemList(pageResult);
        return SysResult.success(pageResult);
    }

    @PutMapping("updateItemStatus")
    public SysResult UpdateStatus(@RequestBody Item item) {
        itemService.updateStatus(item);
        return SysResult.success();
    }

    //未完成
    @PostMapping("addItem")
    public SysResult addItem() {
        return SysResult.success();
    }

    @GetMapping("{id}")
    public SysResult selectById(@PathVariable Integer id){
        Item item = itemService.selectById(id);
        return SysResult.success(item);
    }

    //未完成
    @PutMapping()
    public SysResult UpdateItem() {
        return SysResult.success();
    }

    @DeleteMapping("deleteItemById")
    public SysResult deleteItem(Integer id) {
        System.out.println(id);
        itemService.deleteById(id);
        return SysResult.success();
    }
}
